﻿//author tran trong minh
// date create 20/3/2010 vn

using System.Text.RegularExpressions;
using System;
using System.Web;
using System.Net.Mail;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Solution.Utilities
{
    public static partial class UtilCommon
    {

        public static string VietNamese2English(string unicodeString, bool RemoveSpace, bool KeepSpecialCharacter, params string[] Exception)
        {
            try
            {
                //Remove VietNamese character

                unicodeString = unicodeString.ToLower();
                unicodeString = Regex.Replace(unicodeString, "[áàảãạâấầẩẫậăắằẳẵặ]", "a");
                unicodeString = Regex.Replace(unicodeString, "[éèẻẽẹêếềểễệ]", "e");
                unicodeString = Regex.Replace(unicodeString, "[iíìỉĩị]", "i");
                unicodeString = Regex.Replace(unicodeString, "[óòỏõọơớờởỡợôốồổỗộ]", "o");
                unicodeString = Regex.Replace(unicodeString, "[úùủũụưứừửữự]", "u");
                unicodeString = Regex.Replace(unicodeString, "[yýỳỷỹỵ]", "y");
                unicodeString = Regex.Replace(unicodeString, "[đ]", "d");
                //Remove space
                if (RemoveSpace)
                    unicodeString = unicodeString.Replace(" ", "");
                //Remove special character
                string _remove = "[`~!@#$%^&*()-+=?/>.<,{}[]|]\\]";
                if (Exception != null)
                    foreach (var item in Exception)
                        _remove = _remove.Replace(item, "");
                if (!KeepSpecialCharacter)
                    unicodeString = Regex.Replace(unicodeString, _remove, "");

                return unicodeString;
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static string FilterStringSpecialCharacter(string unicodeString, bool RemoveSpace, params string[] Exception)
        {
            try
            {
                string _remove = "[`~!@#$%^&*()-+=?/>.<,{}[]|]\\]:";
                if (Exception != null)
                    foreach (var item in Exception)
                        _remove = _remove.Replace(item, "");
                unicodeString = Regex.Replace(unicodeString, "[`~!@#$%^&*()-+=?/>.<,{}[]|]\\]:", "");
                //Remove space
                if (RemoveSpace)
                    unicodeString = unicodeString.Replace(" ", "");

                return unicodeString;
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static bool SendMail(string To, string Subject, string body)
        {
            try
            {
                MailMessage message = new MailMessage();

                message.From = new MailAddress("missdlu@missdlu.com");
                message.To.Add(new MailAddress(To));
                message.Subject = Subject;
                message.IsBodyHtml = true;
                message.Body = body;
                message.Priority = MailPriority.High;
                SmtpClient smtp = new SmtpClient("mail.missdlu.com");
                smtp.EnableSsl = true;
                smtp.Credentials = new System.Net.NetworkCredential("missdlu@missdlu.com", "15(sadf)Fx$asfa#Wqcm#");
                smtp.Port = 25;
                smtp.EnableSsl = false;
                smtp.Send(message);
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static string UniqueName(string _input)
        {
            string _out = "";
            _out += DateTime.Now.Year + "_";
            _out += DateTime.Now.Month + "_";
            _out += DateTime.Now.Day + "_";
            _out += DateTime.Now.Hour + "_";
            _out += DateTime.Now.Minute + "_";
            _out += DateTime.Now.Second + "_";
            _out += DateTime.Now.Millisecond + "_";
            _out += _input;
            return _out;
        }
        //   public HttpPostedFileWrapper ImageUploaded { get; set; }

        public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare)
        {
            int newWidth;
            int newHeight;
            Image image = Image.FromStream(imageBuffer);
            int oldWidth = image.Width;
            int oldHeight = image.Height;
            Bitmap newImage;
            if (makeItSquare)
            {
                int smallerSide = oldWidth >= oldHeight ? oldHeight : oldWidth;
                double coeficient = maxSideSize / (double)smallerSide;
                newWidth = Convert.ToInt32(coeficient * oldWidth);
                newHeight = Convert.ToInt32(coeficient * oldHeight);
                Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
                int cropX = (newWidth - maxSideSize) / 2;
                int cropY = (newHeight - maxSideSize) / 2;
                newImage = new Bitmap(maxSideSize, maxSideSize);
                Graphics tempGraphic = Graphics.FromImage(newImage);
                tempGraphic.SmoothingMode = SmoothingMode.AntiAlias;
                tempGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                tempGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                tempGraphic.DrawImage(tempImage, new Rectangle(0, 0, maxSideSize, maxSideSize), cropX, cropY, maxSideSize, maxSideSize, GraphicsUnit.Pixel);
            }
            else
            {
                int maxSide = oldWidth >= oldHeight ? oldWidth : oldHeight;

                if (maxSide > maxSideSize)
                {
                    double coeficient = maxSideSize / (double)maxSide;
                    newWidth = Convert.ToInt32(coeficient * oldWidth);
                    newHeight = Convert.ToInt32(coeficient * oldHeight);
                }
                else
                {
                    newWidth = oldWidth;
                    newHeight = oldHeight;
                }
                newImage = new Bitmap(image, newWidth, newHeight);
            }
            newImage.Save(System.Web.HttpContext.Current.Server.MapPath(savePath + fileName));
            image.Dispose();
            newImage.Dispose();
        }
    }
}
